﻿Imports System.Configuration
Imports HTLibrary
Imports TNCBAL
Imports TNCDAL

Public Class frmLogin
    Inherits DevComponents.DotNetBar.Office2007Form

    Private Sub frmLogin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.AutoValidate = Windows.Forms.AutoValidate.Disable
        Try
            lblServer.Text = GeneralClassBAL.GetGiaTriThanhPhan("Data Source")
            cmbDatabase.Text = GeneralClassBAL.GetGiaTriThanhPhan("Initial Catalog")
            For Each p As String In CollectionHelper.GetDatabases(lblServer.Text)
                cmbDatabase.Items.Add(p)
            Next
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            LoginOK = False
            Exit Sub
        End Try
    End Sub

    Private Sub txtUserName_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtUsername.Validating
        Dim cancel As Boolean
        If String.IsNullOrEmpty(txtUsername.Text) Then
            cancel = True
            Me.ErrorProvider1.SetError(txtUsername, "Tên đăng nhập rỗng")
        End If
        e.Cancel = cancel
    End Sub

    Private Sub txtUserName_Validated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtUsername.Validated
        'Control has validated, clear any error message.
        Me.ErrorProvider1.SetError(txtUsername, String.Empty)
    End Sub

    Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
        'Dim result = GeneralClassDAL.ChangeConnectString(lblServer.Text, cmbDatabase.Text, GeneralClassBAL.GetGiaTriThanhPhan("User ID"), GeneralClassBAL.GetGiaTriThanhPhan("Password"))
        ErrorProvider1.Clear()
        If Me.ValidateChildren(ValidationConstraints.Enabled) Then
            Dim userControl As New UserController
            If Not userControl.DuocPhepChay(txtUsername.Text, txtPassword.Text) Then
                LoginOK = False
                If MessageBox.Show("Đăng nhập không được. Đăng nhập lại?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.Yes Then
                    Me.txtUsername.Text = ""
                    Me.txtPassword.Text = ""
                    Me.txtUsername.Focus()
                End If
                Exit Sub
            End If

            'Đăng nhập thành công
            LoginOK = True

            ''Write log
            'Dim objLogController As New LogController
            'objLogController.Insert(enumAction.LogIn, "Log in")

            'lay ra danh sach cac quyen cua nguoi dung do tren form main
            'truoc tien la xoa het cac quyen cua nguoi su dung truoc
            Me.Close()
        End If
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    Private Sub lblServer_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles lblServer.Validating
        'Dim cancel As Boolean
        'Dim serviceStatus = GeneralClassBAL.CheckServiceStatus()
        'If serviceStatus <> "" Then
        '    cancel = True
        '    Me.ErrorProvider1.SetError(lblServer, serviceStatus)
        'End If
        'e.Cancel = cancel
    End Sub

   
End Class